/**@file client_ipc.c
 * @note 
 * @brief  测试代码
 * 
 * @author   luoxi11
 * @date     20220707
 * @version  V1.0.0
 * 
 * @note ///Description here 
 * @note History:        
 * @note     <author>   <time>    <version >   <desc>
 * @note  
 * @warning  
 */


#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>


#ifndef FOREVER
#define FOREVER for(;;)
#endif


typedef struct msg
{
	char command;                              /* 接收到数据标志位，根据该标志位去判断是否更新数据 */
}report_client_msg;



typedef struct msg_s
{
	char open_diald;
}send_msg_t;


static send_msg_t send_msg;

static report_client_msg rec_msg;


/* 使用socket主动建立一次连接 */
int client_sharemsg()
{
    int sockfd = -1;
    int len = 0;
    struct sockaddr_in address;
    int result = -1;
    char errcnt = 0;
    const char arg[3] = {0};
	char* argv[3];
    char connect_flag = 0;                                        /* 0 未连接上服务端   1 连接上服务端 */

    /* 创建流套接字 */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)
    {
    	printf("socket creat failed \r\n");
    }
    /* 设置要连接的服务器的信息 */
    address.sin_family = AF_INET;                                 /* 使用网络套接字 */
    address.sin_addr.s_addr = inet_addr("127.0.0.1");             /* 服务器地址 */
    address.sin_port = htons(9736);                               /* 服务器所监听的端口 */
    len = sizeof(address);

    

#if 0
    /* 客户端起一个新的进程来启动服务端 */
    int pid;
    if((pid = vfork()) < 0) 
    {
      printf("Can not fork, exit now\n");
    }
      
    if( pid == 0)
    {
      //启动服务端
      int flag =execvp("/server_ipc",argv);
      if(-1 ==flag)
    	{
    	  printf("#### server error is %d  flag is %d ####\n", errno, flag);
    	  perror("connect error is ");
    	}
    }
#endif

    /* 连接到服务器 */
	FOREVER
	{
		if (-1 == sockfd)
		{
			/* 创建流套接字 */
			sockfd = socket(AF_INET, SOCK_STREAM, 0);
			if (-1 == sockfd)
			{
				printf("socket creat failed \r\n");
			}
		}
	    /* 每间隔3秒连接一次          连接成功后保持长连接 */
	    if (0 == connect_flag)
	    {
			if (-1 != sockfd)
		    {
				result = connect(sockfd, (struct sockaddr*)&address, len);
			}
			else
			{
				result = -1;
			}
	    }

		if (result < 0)
		{
		    errcnt++;
			perror("connect error is ");
		}
		else
		{
			errcnt = 0;
			connect_flag = 1;
			
			/* 发送数据给服务器 */
			int send_flag = write(sockfd, &send_msg, sizeof(send_msg_t));
			if(send_flag <= 0)
			{
				printf("write error send_flag is %d\n", send_flag);
			}
			else
			{
				send_msg.open_diald --;
				if(send_msg.open_diald <= 5)
					send_msg.open_diald = 10;

			}
			sleep(1);
			
			/* 从服务器获取数据 */
			int rec_flag=read(sockfd, &rec_msg, sizeof(report_client_msg));
			if(rec_flag <= 0)
			{
				printf("read error send_flag is %d\n", rec_flag);
			}
			else
			{
				printf("rec_msg.command is  %d\r\n", rec_msg.command);
			}
			
		}
		sleep(3);
	}


}

int main(int argv , char *argc[])
{
	client_sharemsg();
	return 0;
}